import Vue from "vue";
import Vuex from "vuex";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { get_setting } from "@/api/index";

Vue.use(Vuex);

const state = {
	setting: "",
};
const getters = {
	setting: (state) => state.setting,
};
const mutations = {
	SET_SETTING: (state, setting) => {
		let sort_info=state.setting.sort_info
		state.setting = setting;
		if (!setting.sort_info) {
			state.setting.sort_info=sort_info
		}
	},
};

const actions = {
	get_setting({ commit }) {
		let data = Object.assign(
			{
				sign: Md5(
					objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
				),
			},
			apiUrl().name
		);
		return new Promise((resolve, reject) => {
			get_setting(data)
				.then((res) => {
					if (res.errcode == 0) {
						commit("SET_SETTING", res.data);
					}
					resolve(res);
				})
				.catch((error) => {
					reject(error);
				});
		});
	},
};

const store = new Vuex.Store({
	state,
	mutations,
	actions,
	getters,
});

export default store;
